Imports Microsoft.VisualBasic
Imports System.Xml
Imports System.Xml.XmlNode
Imports System.Xml.Serialization

<Serializable()> _
Public Class SimulationSetup
    Inherits BaseSetup


    Dim _trainingMap As Hashtable
    Dim _injuriesMap As Hashtable
    Dim _playerMap As Hashtable

    Public Sub New()
        _trainingMap = New Hashtable
        For Each key As String In Cons.GET_SEASONS
            _trainingMap.Add(key, -1)
        Next

        _injuriesMap = New Hashtable

        For Each key As String In Cons.GET_INJURIES

        Next


        _playerMap = New Hashtable
        For Each key As String In Cons.GET_PLAYERS
            _playerMap.Add(key, "-1")
        Next

    End Sub


    Public Property TrainingMap() As Hashtable
        Get
            Return _trainingMap
        End Get
        Set(ByVal value As Hashtable)
            _trainingMap = value
        End Set
    End Property


    Public Property InjuriesMap() As Hashtable
        Get
            Return _injuriesMap
        End Get
        Set(ByVal value As Hashtable)
            _injuriesMap = value
        End Set
    End Property


    Public Property PlayerMap() As Hashtable
        Get
            Return _playerMap
        End Get
        Set(ByVal value As Hashtable)
            _playerMap = value
        End Set
    End Property


    Public Overrides Function SetupToXML() As String
        Dim xml As String = ""

        xml &= "<Setup>"
        xml &= "<TrainingSetup>"
        For Each s As String In Cons.GET_SEASONS
            xml &= "<" & s & ">" & _trainingMap(s) & "</" & s & ">"
        Next
        xml &= "</TrainingSetup>"

        xml &= "<InjuriesSetup>"
        Dim i As Integer = 1
        For Each id As Integer In _injuriesMap.Keys
            xml &= "<Injury" & i & ">"    '<Injury1>
            xml &= "<injuryID>" & id & "</injuryID>"
            Dim ageRanges As Hashtable = _injuriesMap.Item(id)
            For Each a As String In Cons.GET_AGERANGES
                xml &= "<" & a & ">" & ageRanges(a) & "</" & a & ">"   'AgeRange1
            Next


            xml &= "</Injury" & i & ">"    '<Injury1>
            i += 1
        Next

        xml &= "</InjuriesSetup>"


        xml &= "<PlayerSetup>"
        For Each a As String In Cons.GET_PLAYERS
            xml &= "<" & a & ">" & _playerMap(a) & "</" & a & ">"
        Next
        xml &= "</PlayerSetup>"


        xml &= "</Setup>"


        'System.Diagnostics.Debug.WriteLine(xml)
        Return xml
    End Function

    Public Overrides Function XMLToSetup(ByVal xml As String) As BaseSetup
        Dim doc As New XmlDocument

        If (Not xml.Equals("<Setup />")) Then
            doc.LoadXml(xml)

            '_trainingMap = New Hashtable
            Dim value As String
            For Each key As String In Cons.GET_SEASONS
                value = doc.GetElementsByTagName(key).ItemOf(0).InnerText 'training id
                If (IsValidID(value, "TRAINING_TABLE")) Then
                    _trainingMap.Item(key) = value
                Else
                    _trainingMap.Item(key) = -1
                    Update = True
                End If

            Next

            '_injuriesMap = New Hashtable
            For Each key As String In Cons.GET_INJURIES
                Dim ndList As XmlNodeList = doc.GetElementsByTagName(key)   'injury1

                If Not ndList Is Nothing And ndList.Count > 0 Then

                    Dim injuryNode As XmlNode = ndList.ItemOf(0)
                    value = injuryNode.SelectSingleNode("injuryID").InnerText 'id
                    If value.Length = 0 Then
                        value = -1
                    End If

                    If (IsValidID(value, "INJURY_TABLE")) Then
                        Dim ageRangeMap As New Hashtable
                        For Each ar As String In Cons.GET_AGERANGES
                            Dim arv As Integer = injuryNode.SelectSingleNode(ar).InnerText 'id
                            ageRangeMap.Add(ar, arv)
                        Next

                        _injuriesMap.Add(CInt(value), ageRangeMap)
                    Else
                        Update = True
                    End If

                End If


            Next

            '_playerMap = New Hashtable
            For Each key As String In Cons.GET_PLAYERS
                value = doc.GetElementsByTagName(key).ItemOf(0).InnerText
                If (IsValidID(value, "PLAYER_TABLE")) Then
                    _playerMap.Item(key) = value    'player id
                Else
                    _playerMap.Item(key) = -1    'player id
                    Update = True
                End If

            Next
        End If
        Return Me

    End Function

    Public Overrides Function Clone() As BaseSetup

    End Function

    Public Overrides Function ToString() As String

    End Function

    Public Function IsValidID(ByVal id As Integer, ByVal table As String) As Boolean
        Dim _base As Base
        _base = Nothing

        If id = -1 Then
            Return True

        ElseIf (table.Equals("TRAINING_TABLE")) Then

            _base = DBConnection.FET_TRAINING(id)

        ElseIf (table.Equals("INJURY_TABLE")) Then

            _base = DBConnection.FET_INJURY(id)

        ElseIf (table.Equals("PLAYER_TABLE")) Then

            _base = DBConnection.FET_PLAYER(id)

        End If

        If _base Is Nothing Then
            Return False
        Else
            Return True
        End If

    End Function

    Public Overrides Function ValidateBaseSetup() As Collection
        Dim errList As New Collection
        Dim errRow As ErrorRow

        ''go through each training and validate it
        'For Each trainingId As Integer In _trainingMap.Values

        '    Dim training As Base = DBConnection.FET_TRAINING(trainingId)
        '    Dim col As Collection = training.ValidateBase()

        '    For Each err As ErrorRow In col
        '        errList.Add(err)
        '    Next
        'Next

        ''go through each injuries and validate it
        'For Each trainingId As Integer In _trainingMap.Values

        '    Dim training As Base = DBConnection.FET_TRAINING(trainingId)
        '    Dim col As Collection = training.ValidateBase()

        '    For Each err As ErrorRow In col
        '        errList.Add(err)
        '    Next
        'Next



        'go through the trainings making sure something is selected for every season
        For Each trainingId As Integer In _trainingMap.Values

            If trainingId = -1 Then
                errRow = New ErrorRow
                errRow.Field = "Training"
                errRow.Value = trainingId
                errRow.Message = "A training must be seelcted for "
                errList.Add(errRow)
            End If
        Next



        Return errList
        
    End Function

    Public Overrides Function DefaultXML() As String

        Return "<Setup />"
    End Function
End Class
